define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionordertransferlist/index' + location.search,
                    add_url: 'feditionordertransferlist/add',
                    edit_url: 'feditionordertransferlist/edit',
                    del_url: 'feditionordertransferlist/del',
                    multi_url: 'feditionordertransferlist/multi',
                    import_url: 'feditionordertransferlist/import',
                    table: 'fedition_ordertransferlist',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'timedate', title: __('统计开始时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'succnum', title: __('匹配成功订单数'), operate: 'BETWEEN',formatter: Controller.api.formatter.succnum},
                        {field: 'errornum', title: __('未找到订单数'), operate: 'BETWEEN',formatter: Controller.api.formatter.errornum},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('匹配订单'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('点击匹配订单'),
                                    title: __('点击匹配订单'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '等待时间约一分钟，确认开始匹配订单吗？',
                                    url: 'feditionordertransferlist/edit_status',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('对账单'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('对账单总列表'),
                                    title: __('对账单总列表'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-spotify',
                                    url: 'feditionordertransfer/index/?transfer_id={ids}'
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-succnum", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordertransfer/index?type=1&transfer_id=" + ids, __('匹配成功订单'));
            });
            $(document).on("click", ".btn-errornum", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordertransfer/index?type=2&transfer_id=" + ids, __('未找到订单'));
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                succnum: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-succnum" title="匹配成功订单:' + value+ '条">' + value + '</a>';
                },
                errornum: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-errornum" title="未找到订单:' + value+ '条">' + value + '</a>';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});